<?php
/**
 * 前台全局导航菜单
 * Class NavWidget
 */

class NavWidget extends CWidget {

    public $template = "{lis}";

    public $liTemplate = "<li class='menu-item menu-item-style menu-item-style{menu_id} dropdown {current_menu_class}'><a href='{url}' target='{target}'>{title}</a>{sub_menu}</li>";

    public $subTemplate = "<ul class=\"dropdown-menu menu-item-wrap menu-item-col-5\">{lis}</ul>";

    public $subLitemplate = "<li class=\"menu-item {current_menu_class}\"><a href=\"{url}\" target='{target}'>{title}</a></li>";

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        //当视图中执行$this->beginWidget()时候会执行这个方法
        //可以在这里进行查询数据操作
        $nav = Menu::model()->findAll("type='1' AND is_display='1' order by sort ASC, id ASC");
        $navList = Menu::getMenuTree(0, $nav);
        return $navList;

    }

    public function run(){
        //当视图中执行$this->endWidget()的时候会执行这个方法
        //可以在这里进行渲染试图的操作，注意这里提到的视图是widget的视图
        //注意widget的视图是放在跟widget同级的views目录下面，例如下面的视图会放置在
        //  /protected/widget/widgets/views/test.php
        parent::run();
        static $menus = null;
        if( $menus === null ) {
            $menus = Menu::model()->findAll("type='1' AND is_display='1' order by sort ASC, id ASC");
        }
        $content = '';
        foreach ($menus as $key => $menu) {
            /** @var $menu Menu */
            if ($menu->parent_id == 0) {
                $url = $menu->getMenuUrl();
                //var_dump($url);exit;
                $currentMenuClass = '';
                //var_dump(yii::app()->getRequest()->getUrl());
                if ($url == yii::app()->getRequest()->getUrl()) {
                    $currentMenuClass = ' current-menu-item ';
                }
                $submenu = $this->getSubMenu($menus, $menu->id);
                $content .= str_replace([
                    '{menu_id}',
                    '{current_menu_class}',
                    '{url}',
                    '{target}',
                    '{title}',
                    '{sub_menu}'
                ], [
                    $menu->id,
                    $currentMenuClass,
                    $url,
                    $menu->target,
                    $menu->name,
                    $submenu
                ], $this->liTemplate);
            }
        }
        return str_replace('{lis}', $content, $this->template);
    }

    /**
     * @param $menus
     * @param $cur_id
     * @return string|string[]
     */
    private function getSubMenu($menus, $cur_id)
    {
        $content = '';
        foreach ($menus as $key => $menu) {
            /** @var $menu Menu */
            if ($menu['parent_id'] == $cur_id) {
                $url = $menu->getMenuUrl();
                $currentMenuClass = '';
                if ($menu['url'] == Yii::app()->controller->id . '/' . Yii::app()->controller->action->id) {
                    $currentMenuClass = ' active ';
                } else {
                    if (yii::app()->request->getPathInfo() == $menu['url']) {
                        $currentMenuClass = ' active ';
                    }
                }
                $content .= str_replace([
                    '{menu_id}',
                    '{current_menu_class}',
                    '{url}',
                    '{target}',
                    '{title}'
                ], [$menu['id'], $currentMenuClass, $url, $menu->target, $menu->name], $this->subLitemplate);
            }
        }
        if ($content != '') {
            return str_replace('{lis}', $content, $this->subTemplate);
        } else {
            return '';
        }
    }

}